package cn.lxycx.dataterrace.config;
import java.net.URLDecoder;

import javax.servlet.ServletContext;

import cn.lxycx.dataterrace.interceptor.LoginInterceptor;
import cn.lxycx.dataterrace.interceptor.UserLogIntercept;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import cn.lxycx.dataterrace.controller.AccountController;
import cn.lxycx.dataterrace.controller.BackController;
import cn.lxycx.dataterrace.controller.ClassIfyController;
import cn.lxycx.dataterrace.controller.CurdController;
import cn.lxycx.dataterrace.controller.LoadController;
import cn.lxycx.dataterrace.controller.StatementController;
import cn.lxycx.dataterrace.controller.UserInfoController;
import cn.lxycx.dataterrace.model.QueryDao;

public class jfinalConfig extends JFinalConfig{
	
	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		ServletContext context = JFinal.me().getServletContext();
		String proname = PropKit.get("proname","通用数据后台");
		String logintype = PropKit.get("logintype","pass");
		Record conf = new Record().set("proname", proname).set("logintype",logintype);
		context.setAttribute("sysconf",conf);
	}

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config");
		// TODO Auto-generated method stub
		me.setDevMode(PropKit.getBoolean("devMode",false));
		me.setViewType(ViewType.JSP);
		me.setMaxPostSize(1048576000);
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
	//	me.add(new CeshiHandler());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stu
		me.add(new LoginInterceptor());
		me.add(new UserLogIntercept());
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
//		C3p0Plugin cp = new C3p0Plugin("jdbc:oracle:thin:@192.168.2.4:1521:orcl", "hlkj_xc", "HuiLong_Xc", "oracle.jdbc.driver.OracleDriver");
		String driver = PropKit.get("driver");
		
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),driver);
		me.add(dp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		me.add(arp);
		arp.setShowSql(PropKit.getBoolean("showSql",false));//显示sql
		String path = PathKit.getRootClassPath()+"/sqltemp";//URLDecoder.decode(PathKit.getPath(QueryDao.class));
		arp.setBaseSqlTemplatePath(path);
		arp.addSqlTemplate("sqltemp.sql");
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());


		DruidPlugin dp2 = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),PropKit.get("driver"));
		me.add(dp2);
		
		ActiveRecordPlugin arp2 = new ActiveRecordPlugin("data",dp2);
		me.add(arp2);
		arp2.setShowSql(PropKit.getBoolean("showSql",false));//显示sql

		arp2.setBaseSqlTemplatePath(path);
		arp2.addSqlTemplate("sqltemp.sql");
		arp2.setContainerFactory(new OrderedFieldContainerFactory());

		if(driver.indexOf("oracle")>0){
			arp.setDialect(new OracleDialect());
			arp2.setDialect(new OracleDialect());
		}else if(driver.indexOf("mysql")>0){
			arp.setDialect(new MysqlDialect());
			arp2.setDialect(new MysqlDialect());
		}
		
		
	}
	
	
	/*main方法启动时调用*/
	public static void mainInit(){
		PropKit.use("config");
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),PropKit.get("driver"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setShowSql(true);//显示sql
		arp.setDialect(new OracleDialect());
		// 配置属性名(字段名)大小写不敏感容器工厂
		arp.setContainerFactory(new CaseInsensitiveContainerFactory());
		//arp.setContainerFactory(new OrderedFieldContainerFactory());
		String path = URLDecoder.decode(PathKit.getPath(QueryDao.class));
		arp.setBaseSqlTemplatePath(path);
		arp.addSqlTemplate("sqltemp.sql");
		
		dp.setMaxActive(10);
		dp.start();
		arp.start();
	}

	
	
	

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub

		String htmlpath = PropKit.get("htmlpath","/WEB-INF/page");//"/WEB-INF/page";

		me.add("/curd", CurdController.class,htmlpath);
		me.add("/back", BackController.class,htmlpath);
		me.add("/load", LoadController.class,htmlpath);
		me.add("/statement", StatementController.class,htmlpath);
		me.add("/account",AccountController.class,htmlpath);
		me.add("/user", UserInfoController.class,htmlpath);
		me.add("/class",ClassIfyController.class,htmlpath);
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}
}
